package com.faithbj.device.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import com.faithbj.device.dao.ArticleDao;
import com.faithbj.device.model.configuration.Pager;
import com.faithbj.device.model.entity.Article;
import com.faithbj.device.model.entity.ArticleCategory;

/**
 * Dao实现类 - 文章
 * <p>Copyright: Copyright (c) 2011</p> 
 * 
 * <p>Company: www.faithbj.com</p>
 * 
 * @author 	faithbj
 * @date 	2011-12-16
 * @version 1.0
 */

@SuppressWarnings("unchecked")
@Repository("articleDao")
public class ArticleDaoImpl extends BaseDaoImpl<Article, String> implements ArticleDao {
	@Override
	public List<Article> getArticleList(ArticleCategory articleCategory) {
		String hql = "from Article as article where article.isPublication = ? and article.articleCategory.id = ? order by article.createDate desc";
		return getSession().createQuery(hql).setParameter(0, true).setParameter(1, articleCategory.getId()).list();
	}
	@Override
	public List<Article> getArticleList(int firstResult, int maxResults) {
		String hql = "from Article as article where article.isPublication = ? order by article.createDate desc";
		return getSession().createQuery(hql).setParameter(0, true).setFirstResult(firstResult).setMaxResults(maxResults).list();
	}
	
	@Override
	public List<Article> getArticleList(Date beginDate, Date endDate, int firstResult, int maxResults) {
		if (beginDate != null && endDate == null) {
			String hql = "from Article as article where article.isPublication = ? and article.createDate >= ? order by  article.createDate desc";
			return getSession().createQuery(hql).setParameter(0, true).setParameter(1, beginDate).setFirstResult(firstResult).setMaxResults(maxResults).list();
		} else if (endDate != null && beginDate == null) {
			String hql = "from Article as article where article.isPublication = ? and article.createDate <= ? order by article.createDate desc";
			return getSession().createQuery(hql).setParameter(0, true).setParameter(1, endDate).setFirstResult(firstResult).setMaxResults(maxResults).list();
		} else if (endDate != null && beginDate != null) {
			String hql = "from Article as article where article.isPublication = ? and article.createDate >= ? and article.createDate <= ? order by article.createDate desc";
			return getSession().createQuery(hql).setParameter(0, true).setParameter(1, beginDate).setParameter(2, endDate).setFirstResult(firstResult).setMaxResults(maxResults).list();
		} else {
			String hql = "from Article as article where article.isPublication = ? order by article.createDate desc";
			return getSession().createQuery(hql).setParameter(0, true).setFirstResult(firstResult).setMaxResults(maxResults).list();
		}
	}
	@Override
	public Pager getArticlePager(ArticleCategory articleCategory, Pager pager) {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Article.class);
		detachedCriteria.createAlias("articleCategory", "articleCategory");
		detachedCriteria.add(Restrictions.eq("articleCategory", articleCategory));
		detachedCriteria.add(Restrictions.eq("isPublication", true));
		return super.findByPager(pager, detachedCriteria);
	}
	@Override
	public List<Article> getRecommendArticleList(int maxResults) {
		String hql = "from Article as article where article.isPublication = ? and article.isRecommend = ? order by article.createDate desc";
		return getSession().createQuery(hql).setParameter(0, true).setParameter(1, true).list();
	}
	@Override
	public List<Article> getRecommendArticleList(ArticleCategory articleCategory, int maxResults) {
		String hql = "from Article as article where article.isPublication = ? and article.isRecommend = ? and (articleCategory = ? or article.articleCategory.id = ?) order by article.createDate desc";
		return getSession().createQuery(hql).setParameter(0, true).setParameter(1, true).setParameter(2, articleCategory).setParameter(3, articleCategory.getId()).list();
	}
	@Override
	public List<Article> getHotArticleList(int maxResults) {
		String hql = "from Article as article where article.isPublication = ? order by article.createDate desc";
		return getSession().createQuery(hql).setParameter(0, true).list();
	}
	@Override
	public List<Article> getHotArticleList(ArticleCategory articleCategory, int maxResults) {
		String hql = "from Article as article where article.isPublication = ? and (articleCategory = ? or article.articleCategory.id = ?) order by article.createDate desc";
		return getSession().createQuery(hql).setParameter(0, true).setParameter(1, articleCategory).setParameter(2, articleCategory.getId()).list();
	}
	@Override
	public List<Article> getNewArticleList(int maxResults) {
		String hql = "from Article as article where article.isPublication = ? order by article.createDate desc";
		return getSession().createQuery(hql).setParameter(0, true).setFirstResult(0).setMaxResults(maxResults).list();
	}

	public List<Article> getNewArticleList(ArticleCategory articleCategory, int maxResults) {
		String hql = "from Article as article where article.isPublication = ? and (articleCategory = ? or article.articleCategory.id = ?) order by article.createDate desc";
		return getSession().createQuery(hql).setParameter(0, true).setParameter(1, articleCategory).setParameter(2, articleCategory.getId()).setFirstResult(0).setMaxResults(maxResults).list();
	}
	
	// 根据isTop、createDate进行排序
	@Override
	public List<Article> getAll() {
		String hql = "from Article as article order by article.createDate desc";
		return getSession().createQuery(hql).list();
	}

	@Override
	public List<Article> getHelpArticleList(int maxResults) {
		String hql = "from Article as article where article.isHelp = ? order by article.createDate desc";
		return getSession().createQuery(hql).setParameter(0, true).setFirstResult(0).setMaxResults(maxResults).list();
	}

	@Override
	public List<Article> getArticleList(String articleCategory,
			int firstResult, int maxResults) {
//		
//		ist   cats   =   getSession().createCriteria(Article.class) 
//		        .add(Restrictions.eq( "isPublication ",   true) 
//		        .addOrder(   Order.asc( "name ")   ) 
//		        .addOrder(   Order.desc( "age ")   ) 
//		        .setMaxResults(50) 
//		        .list();
		
		
		
		String hql = "from Article as a where a.isPublication = ? and a.articleCategory.id = ?";
//		String hql = "from Article as article where article.isPublication = ? and article.articleCategory.id = ? order by article.createDate desc";
		return getSession().createQuery(hql).setParameter(0, true).setParameter(1, articleCategory).setFirstResult(firstResult).setMaxResults(maxResults).list();

	}

}
